<template>
  <el-row v-loading="loading">
    <el-col :span="12">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>医院</span>
        </div>
        <el-tree :data="hospitalData" :props="defaultProps" @node-click="hospitalClick"></el-tree>
      </el-card>
    </el-col>
    <el-col :span="12">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>科室</span>
        </div>
        <el-tree :data="deptData" :props="defaultProps" @node-click="deptClick"></el-tree>
      </el-card>
    </el-col>
  </el-row>
</template>
<script>
import { mapActions } from 'vuex'
export default {
  name: 'Org',
  components: { },
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      loading: false,
      hospitalData: [],
      deptData: []
    }
  },
  methods: {
    ...mapActions(['GetHospitalTree', 'GetDeptTree']),
    hospitalClick(data) {
      this.hospitalId = data.id
      this.searchDept()
    },
    deptClick(data) {
    },
    searchHospital() {
      this.GetHospitalTree().then(res => {
        if (res) {
          this.hospitalData = res
        }
      }).catch(() => {
      })
    },
    searchDept() {
      this.loading = true
      this.GetDeptTree(this.hospitalId).then(res => {
        if (res) {
          this.deptData = res
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    }
  },
  mounted() {
    this.searchHospital()
  }
}
</script>
